package com.hnluchuan.staff.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hnluchuan.core.utils.ValidationUtils;
import org.apache.commons.lang3.StringUtils;
import com.hnluchuan.staff.dao.DepartmentDAO;
import com.hnluchuan.staff.model.Department;
import com.hnluchuan.staff.dto.DepartmentDTO;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.utils.common.BeanMapper;

@Service
public class DepartmentService extends BaseService {
	
	@Autowired
	private DepartmentDAO departmentDAO;

	public List<DepartmentDTO> find(DepartmentDTO query, Page page) {
		List<Department> models = departmentDAO.find(query, page);
		List<DepartmentDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DepartmentDTO> findAll() {
		return this.find(new DepartmentDTO(), null);
	}
	
	public List<DepartmentDTO> findAll(DepartmentDTO query) {
		return this.find(query, null);
	}
	
	public List<DepartmentDTO> find(Page page) {
		return this.find(new DepartmentDTO(), page);
	}
	
	public List<DepartmentDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DepartmentDTO(), page);
	}
	
	public Long create(DepartmentDTO dto) {
		Department model = new Department();
		toModel(model, dto);

		ValidationUtils.assertTrue(StringUtils.isNotBlank(model.getName()), "部门名不能为空");
		return departmentDAO.create(model);
	}
	
	public DepartmentDTO load(Long id) {
	    Department model = departmentDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(DepartmentDTO dto) {
		Department model = departmentDAO.load(dto.getId());
		toModel(model, dto);
		departmentDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {


		if (ids != null) {
			for (Long id : ids) {
				departmentDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public DepartmentDTO findOne(DepartmentDTO query) {
		Department model = departmentDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DepartmentDTO> toDTOs(List<Department> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DepartmentDTO>(0);
		}
		List<DepartmentDTO> dtos = new ArrayList<DepartmentDTO>(models.size());
		for (Department model : models) {
	        DepartmentDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DepartmentDTO toDTO(Department model) {
		if (model == null) {
			return null;
		}
		DepartmentDTO dto = BeanMapper.map(model, DepartmentDTO.class);
		
		return dto;
	}
	
	private void toModel(Department model, DepartmentDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Department> toModels(List<DepartmentDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Department>(0);
		}
		List<Department> models = new ArrayList<Department>(dtos.size());
		for (DepartmentDTO dto : dtos) {
	        Department model = new Department();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
